async function selectSql(sql: string, config = {} as any) {
  // console.log('selectSql');
  // eslint-disable-next-line no-async-promise-executor
  return new Promise(async callback => {
    const defaultConfig = {
      name: 'chat',
      path: '_doc/chat.db'
    }
    config = { ...defaultConfig, ...config }
    // 1.判断是否打开数据库
    if (!plus.sqlite.isOpenDatabase(config)) {
      // 2.如果没打开先打开
      const { code, msg } = await openDB(config)
      if (!code) {
        return [0, msg]
      }
    }
    // 执行数据库查询
    plus.sqlite.selectSql({
      name: config.name,
      sql,
      success(data) {
        callback({ code: 1, data, msg: 'selectSql success' })
      },
      fail(e) {
        callback({ code: 0, data: [], msg: `selectSql failed: ${JSON.stringify(e)}` })
      }
    })
  })
}

async function executeSql(sql: undefined | string[], config = {} as any) { // 执行executeSql
  // console.log('executeSql');
  const defaultConfig = {
    name: 'chat',
    path: '_doc/chat.db'
  }
  config = { ...defaultConfig, ...config }
  // 1.判断是否打开数据库
  if (!plus.sqlite.isOpenDatabase(config)) {
    // 2.如果没打开先打开
    const { code, msg } = await openDB(config)
    if (!code) {
      return [0, msg]
    }
  }
  return new Promise(callback => {
    plus.sqlite.executeSql({
      name: config.name,
      sql,
      success(e) {
        callback({ code: 1, msg: e })
      },
      fail(e) {
        callback({ code: 0, msg: `executeSql failed: ${JSON.stringify(e)}` })
      }
    })
  })
}

// 打开数据库
async function openDB(config: any): Promise<{ code: number, msg: string }> {
  return new Promise((callback) => {
    plus.sqlite.openDatabase({
      ...config,
      success(e) {
        callback({ code: 1, msg: 'openDatabase success!' })
      },
      fail(e) {
        callback({ code: 0, msg: `openDatabase failed: ${JSON.stringify(e)}` })
      }
    })
  })
}
export default { selectSql, executeSql, openDB }
